package dom;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;

public class XMLParse2 {
    Document doc=null;
    public void getDocument(){
        // 获取解析器工厂
        DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();

        // 获取解析器
        try {
            DocumentBuilder bd=dbf.newDocumentBuilder();
            doc= bd.parse("src/main/resources/收藏信息.xml");
        } catch (ParserConfigurationException e) {
            e.printStackTrace();;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        }
    }

    public void showInfo(){
        // 获取<Brand>的所有节点集合
        NodeList nodeList=doc.getElementsByTagName("pubDate");
        // 获取第一个元素
        Element pubDateElement=(Element) nodeList.item(0);
        // 获取最后一个节点的value
        String pubDate=pubDateElement.getFirstChild().getNodeValue();
        System.out.println(pubDate);
    }

    public static void main(String[] args) {
        XMLParse2 parse=new XMLParse2();
        parse.getDocument();
        parse.showInfo();
    }
}
